﻿@using eLoan.Models;
@model LoanPenaltyModel

@{
    ViewBag.Title = "Loan Penalty";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Loan Penalty</h2>
        <ol class="breadcrumb">
            <li>
                Workspace
            </li>
            <li class="active">
                <strong>Loan Penalty</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>

@using (Html.BeginForm("SaveLoanPenalty", "Workspace", FormMethod.Post, new { id = "saveForm" }))
{
    <fieldset>
        <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins">
                        <div class="ibox-title">
                            <div class="divSearch">
                                <table class="table-search">
                                    <tr>
                                        <th>
                                            <span>@Html.LabelFor(m => m.Branches)</span>
                                        </th>
                                        <th>
                                            <span class="span">@Html.LabelFor(m => m.SearchText)</span>
                                        </th>
                                        <th></th>
                                    </tr>
                                    <tr>
                                        <td>
                                            @Html.DropDownListFor(m => m.SelectedBranchCode, Model.Branches, new { @id = "ddlBranches", @class = "single-line", @width = "50%" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.SearchText, new { @id = "txtSearchText", @class = "single-line", @width = "50%" })
                                        </td>
                                        <td style="vertical-align:bottom">
                                            <button type="submit" id="btnSearch" class="btn btn-primary btnSearch" name="command" value="Refresh">Search</button>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <div class="ibox-title">
                            <div class="pull-left"><h3>Loans with Penalty</h3></div>
                            <div class="pull-right">
                                <h3>
                                    Total Penalty: &nbsp;&nbsp;&nbsp; <span id="total">@Html.ValueFor(m => Model.TotalPenalty, "{0:#,###,##0.00}")</span>
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
                                    Total Paid Penalty: &nbsp;&nbsp;&nbsp; <span id="total">@Html.ValueFor(m => Model.TotalPaidPenalty, "{0:#,###,##0.00}")</span>
                                </h3>
                            </div>
                        </div>
                        <div class="ibox-content">
                            <table class="table table-bordered table-List">
                                <thead>
                                    <tr>
                                        <th>Borrower</th>
                                        <th>Document No</th>
                                        <th>Receivable</th>
                                        <th>Total Payment</th>
                                        <th>Outstanding Balance</th>
                                        <th>Due Date</th>
                                        <th>Last Payment</th>
                                        <th>Total Penalty</th>
                                        <th>Total Additional Interest</th>
                                        <th>Last Penalty Date</th>
                                        <th>Total Paid Penalty</th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody id="tableBody">
                                    @for (var i = 0; i < Model.PenaltyList.Count(); i++)
                    {
                                    <tr class="trData">
                                        <td>
                                            @Html.HiddenFor(m => m.PenaltyList[i].BorrowerName)
                                            @Html.ActionLink(Model.PenaltyList[i].BorrowerName, "LoanJournal", "Loan", null, null, null, new { documentNo = Model.PenaltyList[i].DocumentNo }, null)
                                        </td>
                                        <td>
                                            @Html.HiddenFor(m => m.PenaltyList[i].DocumentNo)
                                            @Html.ActionLink(Model.PenaltyList[i].DocumentNo, "LoanDetails", "Loan", null, null, null, new { documentNo = Model.PenaltyList[i].DocumentNo }, null)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].Receivable, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].Receivable)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].TotalPayment, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].TotalPayment)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].OutstandingBalance, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].OutstandingBalance)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].DueDate, "{0:MMM/dd/yyyy}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].DueDate)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].LastPaymentDate, "{0:MMM/dd/yyyy}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].LastPaymentDate)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].TotalPenalty, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].TotalPenalty)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].TotalAdditionalInterest, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].TotalAdditionalInterest)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].LastPenaltyDate, "{0:MMM/dd/yyyy}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].LastPenaltyDate)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.PenaltyList[i].TotalPaidPenalty, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.PenaltyList[i].TotalPaidPenalty)
                                        </td>
                                        <td>
                                            @Html.ActionLink("Print Demand Letter", "PrintDemandLetter", "Workspace", new { documentNo = @Model.PenaltyList[i].DocumentNo, branchCode = @Model.SelectedBranchCode, searchText = @Model.SearchText, command = "Penalty" }, null)
                                        </td>
                                    </tr>
                    }
                                </tbody>
                            </table>
                        </div>

                        <div class="ibox-title">
                            <div class="pull-left"><h3>Delayed Loans</h3></div>
                            <div class="ibox-tools">
                                <button type="button" class="btn btn-primary btn-sm" name="command" value="SaveDelayed" data-toggle="modal" data-target="#confirm-delayed">Penalize Delayed Loans</button>
                                <button type="submit" id="btnSubmitDelayed" style="display:none" name="command" value="SaveDelayed"></button>
                            </div>
                        </div>
                        <div class="ibox-content">
                            <table class="table table-bordered table-List">
                                <thead>
                                    <tr>
                                        <th>Borrower</th>
                                        <th>Document No</th>
                                        <th>Maturity Date</th>
                                        <th>Receivable</th>
                                        <th>Outstanding Balance</th>
                                        <th>Due Date</th>
                                        <th>Total Payment</th>
                                        <th>Last Payment</th>
                                        <th>Short / Over</th>
                                        <th>Penalty</th>
                                        <th>Additional Interest</th>
                                        <th>Remarks</th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody id="tableBody">
                                    @for (var i = 0; i < Model.DelayedLoans.Count(); i++)
                                    {
                                    <tr class="trData">
                                        <td>
                                            @Html.HiddenFor(m => m.DelayedLoans[i].BorrowerName)
                                            @Html.ActionLink(Model.DelayedLoans[i].BorrowerName, "LoanJournal", "Loan", null, null, null, new { documentNo = Model.DelayedLoans[i].DocumentNo }, null)
                                        </td>
                                        <td>
                                            @Html.HiddenFor(m => m.DelayedLoans[i].DocumentNo)
                                            @Html.ActionLink(Model.DelayedLoans[i].DocumentNo, "LoanDetails", "Loan", null, null, null, new { documentNo = Model.DelayedLoans[i].DocumentNo }, null)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.DelayedLoans[i].MaturityDate, "{0:MMM/dd/yyyy}")
                                            @Html.HiddenFor(m => m.DelayedLoans[i].MaturityDate)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.DelayedLoans[i].Receivable, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.DelayedLoans[i].Receivable)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.DelayedLoans[i].OutstandingBalance, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.DelayedLoans[i].OutstandingBalance)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.DelayedLoans[i].DueDate, "{0:MMM/dd/yyyy}")
                                            @Html.HiddenFor(m => m.DelayedLoans[i].DueDate)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.DelayedLoans[i].TotalPayment, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.DelayedLoans[i].TotalPayment)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.DelayedLoans[i].LastPaymentDate, "{0:MMM/dd/yyyy}")
                                            @Html.HiddenFor(m => m.DelayedLoans[i].LastPaymentDate)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.DelayedLoans[i].ShortOver, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.DelayedLoans[i].ShortOver)
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.DelayedLoans[i].TotalPenalty, "{0:#,###,##0.00}", new { @class = "single-line scroll-field1", @onchange = "FormatMe(this);" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.DelayedLoans[i].TotalAdditionalInterest, "{0:#,###,##0.00}", new { @class = "single-line scroll-field2", @onchange = "FormatMe(this);" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.DelayedLoans[i].Remarks, new { @class = "single-line scroll-field3" })
                                        </td>
                                        <td>
                                            @Html.ActionLink("Print Demand Letter", "PrintDemandLetter", "Workspace", new { documentNo = @Model.DelayedLoans[i].DocumentNo, branchCode = @Model.SelectedBranchCode, searchText = @Model.SearchText, command = "Delayed" }, null)
                                        </td>
                                    </tr>
                                    }
                                </tbody>
                            </table>

                            <div class="modal fade" id="confirm-delayed" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">

                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title" id="myModalLabel">Confirmation</h4>
                                        </div>

                                        <div class="modal-body">
                                            <p>Are you sure you want to penalize delayed laons?</p>
                                            <p class="debug-url"></p>
                                        </div>

                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
                                            <button type="button" class="btn btn-primary btn-ok" data-dismiss="modal">Yes</button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            

                        </div>

                        <div class="ibox-title">
                            <div class="pull-left"><h3>Delinquent Loans</h3></div>
                            <div class="ibox-tools">
                                <button type="button" class="btn btn-primary btn-sm" name="command" value="SaveDelinquent" data-toggle="modal" data-target="#confirm-delinquent">Penalize Delinquent Loans</button>
                                <button type="submit" id="btnSubmitDelinquent" style="display:none" name="command" value="SaveDelinquent"></button>
                            </div>
                        </div>
                        <div class="ibox-content">
                            <table class="table table-bordered table-List">
                                <thead>
                                    <tr>
                                        <th>Borrower</th>
                                        <th>Document No</th>
                                        <th>Maturity Date</th>
                                        <th>Receivable</th>
                                        <th>Outstanding Balance</th>
                                        <th>Due Date</th>
                                        <th>Total Payment</th>
                                        <th>Last Payment</th>
                                        <th>Short / Over</th>
                                        <th>Penalty</th>
                                        <th>Additional Interest</th>
                                        <th>Remarks</th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody id="tableBody">
                                    @for (var i = 0; i < Model.DelinquentLoans.Count(); i++)
                                    {
                                        <tr class="trData">
                                            <td>
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].BorrowerName)
                                                @Html.ActionLink(Model.DelinquentLoans[i].BorrowerName, "LoanJournal", "Loan", null, null, null, new { documentNo = Model.DelinquentLoans[i].DocumentNo }, null)
                                            </td>
                                            <td>
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].DocumentNo)
                                                @Html.ActionLink(Model.DelinquentLoans[i].DocumentNo, "LoanDetails", "Loan", null, null, null, new { documentNo = Model.DelinquentLoans[i].DocumentNo }, null)
                                            </td>
                                            <td>
                                                @Html.ValueFor(m => m.DelinquentLoans[i].MaturityDate, "{0:MMM/dd/yyyy}")
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].MaturityDate)
                                            </td>
                                            <td>
                                                @Html.ValueFor(m => m.DelinquentLoans[i].Receivable, "{0:#,###,##0.00}")
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].Receivable)
                                            </td>
                                            <td>
                                                @Html.ValueFor(m => m.DelinquentLoans[i].OutstandingBalance, "{0:#,###,##0.00}")
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].OutstandingBalance)
                                            </td>
                                            <td>
                                                @Html.ValueFor(m => m.DelinquentLoans[i].DueDate, "{0:MMM/dd/yyyy}")
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].DueDate)
                                            </td>
                                            <td>
                                                @Html.ValueFor(m => m.DelinquentLoans[i].TotalPayment, "{0:#,###,##0.00}")
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].TotalPayment)
                                            </td>
                                            <td>
                                                @Html.ValueFor(m => m.DelinquentLoans[i].LastPaymentDate, "{0:MMM/dd/yyyy}")
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].LastPaymentDate)
                                            </td>
                                            <td>
                                                @Html.ValueFor(m => m.DelinquentLoans[i].ShortOver, "{0:#,###,##0.00}")
                                                @Html.HiddenFor(m => m.DelinquentLoans[i].ShortOver)
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => m.DelinquentLoans[i].TotalPenalty, "{0:#,###,##0.00}", new { @class = "single-line scroll-field4", @onchange = "FormatMe(this);" })
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => m.DelinquentLoans[i].TotalAdditionalInterest, "{0:#,###,##0.00}", new { @class = "single-line scroll-field5", @onchange = "FormatMe(this);" })
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => m.DelinquentLoans[i].Remarks, new { @class = "single-line scroll-field6" })
                                            </td>
                                            <td>
                                                @Html.ActionLink("Print Demand Letter", "PrintDemandLetter", "Workspace", new { documentNo = @Model.DelinquentLoans[i].DocumentNo, branchCode = @Model.SelectedBranchCode, searchText = @Model.SearchText, command = "Delinquent" }, null)
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>

                            <div class="modal fade" id="confirm-delinquent" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">

                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title" id="myModalLabel">Confirmation</h4>
                                        </div>

                                        <div class="modal-body">
                                            <p>Are you sure you want to penalize delinquent loans?</p>
                                            <p class="debug-url"></p>
                                        </div>

                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
                                            <button type="button" class="btn btn-primary btn-ok" data-dismiss="modal">Yes</button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            

                        </div>

                    </div>

                </div>
            </div>
        </div>
    </fieldset>
}
@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")

    <style>

    .table-search {
        width: 100%;
        border: none;
    }

        .table-search th {
            width: 250px;
        }

            .table-search th label {
                margin-top: 15px;
                margin-left: 20px;
            }

            .table-search th .span {
                padding-left: 10px;
            }

        .table-search .align-right{
            text-align: right;
        }

            .table-search td select {
                margin-left: 15px !important;
                margin-bottom: 10px !important;
            }

            .table-search td input {
                margin-left: 25px !important;
                margin-bottom: 10px !important;
            }

            .table-search td button {
                margin-right: 15px;
                margin-bottom: 10px !important;
            }

        .table-search td .btnSearch {
            margin-bottom: 0px;
            margin-left: 40px !important;
        }

    .divSearch {
        background-color: #F5F5F6;
        border: 1px solid #EBEBEB;
    }

</style>

}

@section Scripts {
@Scripts.Render("~/plugins/dataTables")
<script type="text/javascript">

    $(document).ready(function () {

        $('.table-List').dataTable({
            ordering: true,
            searching: false,
            paging: false,
            scrollY: 300
        });


        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                Refresh();
                return false;
            }
        });

        $('#confirm-delayed').on('click', '.btn-ok', function (e) {
            document.getElementById('btnSubmitDelayed').click();
        });

        $('#confirm-delinquent').on('click', '.btn-ok', function (e) {
            document.getElementById('btnSubmitDelinquent').click();
        });

    });

    function Refresh() {
        document.getElementById('btnRefresh').click();
    }

    function GetTotal()
    {
        var totalAmount = 0;
        $('.amount').each(function () {
            var val = $(this).val().replace(',','');
            
            totalAmount = parseFloat(totalAmount) + parseFloat(val);

        });

        var total = $('#total');
        total.text(FormatNumber(totalAmount.toFixed(2)));
    }

    function FormatNumber(nStr) {
        nStr += '';
        var x = nStr.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }

    function FormatMe(me) {
        var val = $(me).val().replace(',', '').replace(',', '').replace(',', '');

        if (isNaN(parseFloat(val)))
            val = 0.00;

        var decimalFormat = parseFloat(val).toFixed(2);
        var commaFormat = FormatNumber(decimalFormat);

        $(me).val(commaFormat);
    }

    function SortList(orderBy)
    {
        var hdnOrderBy = $('#hdnOrderBy').val();
        var hdnOrder = $('#hdnOrder').val();

        

        if (orderBy == hdnOrderBy) {
            if (hdnOrder == 'Asc')
                $('#hdnOrder').val('Desc');
            else
                $('#hdnOrder').val('Asc');
        }
        else {
            $('#hdnOrderBy').val(orderBy);
            $('#hdnOrder').val('Asc');
        }

        Refresh();

    }

    function GetAgents()
    {
        var val = $('#ddlBranches').val();
        var subItems = "";

        $.getJSON("@Url.Action("GetAgents","Borrower")", { branchCode: val },
    function (data) {

        $.each(data, function (index, item) {

            subItems += "<option value='" + $.trim(item.Value) + "'>" + $.trim(item.Text) + "</option>"

        });

        $("#ddlAgents").html(subItems)
    });
    }
</script>

}
